package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.itsource.constants.RConst;
import cn.itsource.pojo.domain.CustomerCar;
import cn.itsource.mapper.CustomerCarMapper;
import cn.itsource.pojo.ro.UserCarRo;
import cn.itsource.pojo.vo.UserCarVo;
import cn.itsource.service.ICustomerCarService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户车辆 服务实现类
 * </p>
 *
 * @author whale
 * @since 2024-07-08
 */
@Service
public class CustomerCarServiceImpl extends ServiceImpl<CustomerCarMapper, CustomerCar> implements ICustomerCarService {
    //用户添加车辆
    @Override
    public void addUserCar(UserCarRo userCarRo) {
        super.save(buildCar(userCarRo));
    }


      //查询车辆
    @Override
    public List<UserCarVo> getUserCars() {
        //怎么去查询当前用户的车辆
        //先根据登录id去找到乘客的id
        Long customerId = Long.valueOf(StpUtil.getLoginId().toString());
        //找到乘客id过后 根据乘客id去找对应的乘客车辆 车俩有可能有多个 但乘客id只有一个
        List<CustomerCar> list = super.list(new LambdaQueryWrapper<CustomerCar>()
                .eq(CustomerCar::getCustomerId, customerId));
        //最后把查出来的车辆全部返回
        return UserCarVo.CustomerCarToUserCars(list);
    }

    private static CustomerCar buildCar(UserCarRo userCarRo) {
        //创建一个车辆信息
        CustomerCar customerCar = new CustomerCar();
        //设置id车辆
        long id = IdUtil.createSnowflake(RConst.Num.INT_1, RConst.Num.INT_1).nextId();
        customerCar.setId(id);
        customerCar.setCustomerId(Long.valueOf(StpUtil.getLoginId().toString()));
        customerCar.setCarType(userCarRo.getCarType());
        customerCar.setCarPlate(userCarRo.getCarPlate());
        customerCar.setCreateTime(new Date());
        return customerCar;
    }
}
